package com.choosefine.it.manager.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.choosefine.it.manager.model.ManagerProjectExperience;
import com.choosefine.it.manager.model.vo.ManagerProjectExperienceVo;
import com.choosefine.it.manager.service.ManagerProjectExperienceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author 潘钱钦 上午11:50:11
 * 
 */
@RestController
@RequestMapping("/manager/managerProjectExperience")
@Api(value = "项目经理项目经验查询列表", description = "潘钱钦")
public class ManagerProjectExperienceController extends BaseController {
	@Autowired
	private ManagerProjectExperienceService managerProjectExperienceService;
	@Autowired
	CustomRestTemplate restTemplate;
	@Autowired
	ObjectMapper jsonObjectMapper;
	Logger logger = LoggerFactory.getLogger(ManagerProjectExperienceController.class);
	@Value("#{yaml.get('basedata.projectinfo.getProjectInfoByCode')}")
	final String projectinfoURL = null;
	@Value("#{yaml.get('basedata.finishPhoto.getFinishPhotoByUserCodeAndProjectCode')}")
	final String finishPhotoURL = null;
	@Value("#{yaml.get('basedata.projectType.getProjectTypeById')}")
	final String projectTypeURL = null;
	
	@ApiOperation(value = "根据用户的usercode查看项目经验列表", notes = "",response=ManagerProjectExperienceVo.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "用户的唯一编号", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "query") })
	@RequestMapping(value = "/getManagerProjectExperience/{userCode}", method = RequestMethod.GET)
	public String getManagerProjectExperience(@PathVariable String userCode,
			Integer pageNum, Integer pageSize) throws JsonProcessingException {
		String result = null;
		if (StringUtils.isNotBlank(userCode)) {
			PageDTO page = new PageDTO();
			page.setPageNum(pageNum);
			page.setPageSize(pageSize);
			Page<ManagerProjectExperience> list = managerProjectExperienceService
					.selectManagerProjectExperienceByUserCode(userCode, page);
			int projectCount=managerProjectExperienceService.ManagerProjectCount(userCode);
			
			Page<ManagerProjectExperienceVo> mlist = new Page<ManagerProjectExperienceVo>(pageNum,pageSize);
			mlist.setPages(list.getPages());
			ManagerProjectExperienceVo mpVo= null;
			if (list.size() > 0) {
				for (ManagerProjectExperience m : list) {
					mpVo=new ManagerProjectExperienceVo();
					StringBuffer url = URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  projectinfoURL);
					url.append(m.getProjectCode());
					Map projectInfoResultMap = restTemplate.getForObject(url.toString(),Map.class, new HashMap<>());
					Map projectInfo = (Map) projectInfoResultMap.get("data");
					logger.info("序列化结果 :\n"+ jsonObjectMapper.writeValueAsString(projectInfo));
					if(projectInfo!=null){//判断项目信息是否为空
						String projectName = (String) projectInfo.get("projectName");
						String constructionUnit = (String) projectInfo.get("constructionUnit");
						Long projectStart = (Long) projectInfo.get("projectStart");
						Long projectEnd = (Long) projectInfo.get("projectEnd");
						Integer projectType = (Integer) projectInfo.get("projectType");
						String projectAddress = (String) projectInfo.get("projectAddress");
						String projectScale = (String) projectInfo.get("projectScale");
						String projectTypeUrl=URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  projectTypeURL).append(projectType).toString();

						Map projectTypeResultMap = restTemplate.getForObject(projectTypeUrl,Map.class, new HashMap<>());
						Map projectTypeResult = (Map) projectTypeResultMap.get("data");
						String projectTypeName=null;
						if(projectTypeResult!=null){
							projectTypeName= projectTypeResult.get("projectType").toString();
						}
						mpVo.setId(m.getId());
						mpVo.setEvaluationIdorganizationskills(m.getEvaluationIdorganizationskills());
						mpVo.setCredit(m.getCredit());
						mpVo.setConstructionUnit(constructionUnit);
						mpVo.setProjectAddress(projectAddress);
						mpVo.setProjectType(projectType);
						mpVo.setProjectTypeName(projectTypeName);
						mpVo.setProjectStart(projectStart);
						mpVo.setProjectScale(projectScale);
						mpVo.setProjectName(projectName);
						mpVo.setProjectEnd(projectEnd);
						String projectCodeAndUserCode = m.getUserCode() + "/"
								+ m.getProjectCode();
						String urlPhoto = URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  finishPhotoURL).append(projectCodeAndUserCode).toString();
						List<String> finishPhotoResult = restTemplate.getForObject(urlPhoto, List.class, new HashMap<>());
						mpVo.setFinishPhotoUrl(finishPhotoResult);
						mpVo.setProjectCount(projectCount);
						mlist.add(mpVo);
					}
				}
				result = responseSuccess(mlist);
			} else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
			}

		} else {
			result = responseFail(ResultCode.SERVERERROR_INTERNAL, ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

}
